package com.learn.xueer.ui.activity

import android.os.Bundle
import androidx.lifecycle.Observer
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.learn.xueer.app.base.BaseActivity
import com.learn.xueer.data.model.bean.Notice
import com.learn.xueer.databinding.ActivityContactServerBinding
import com.learn.xueer.databinding.ActivityMessageBinding
import com.learn.xueer.databinding.ActivityWorkMessageBinding
import com.learn.xueer.ui.adapter.NoticeAdapter
import com.learn.xueer.ui.adapter.WorkMessageAdapter
import com.learn.xueer.ui.viewmodel.MessageViewModel
import com.learn.xueer.ui.viewmodel.MineViewModel
import me.hgj.jetpackmvvm.util.get

/**
 * 消息
 */
class WorkMessageActivity : BaseActivity<MessageViewModel, ActivityWorkMessageBinding>() {

    private val workMessageAdapter: WorkMessageAdapter by lazy { WorkMessageAdapter(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.messageReq()
        mDatabind.click = ProxyClick()

        mDatabind.rv.adapter = workMessageAdapter
        var innerJson = intent.get<String>("data").toString()
        val innerData = Gson().fromJson<MutableList<Notice>>(
            innerJson,
            object : TypeToken<MutableList<Notice>>() {}.type
        )
        workMessageAdapter.setList(innerData)
    }


    override fun createObserver() {
        mViewModel.messageResult.observe(this, Observer {
            if (it.message.isNotEmpty()) {
                mViewModel.messageContent.value = it.message[0].content
                mViewModel.messageTime.value = it.message[0].create_time_text
            }
            if (it.notice.isNotEmpty()) {
                mViewModel.noticeContent.value = it.notice[0].content
                mViewModel.noticeTime.value = it.notice[0].create_time_text
            }

        })
    }

    inner class ProxyClick {
        fun back() {
            finish()
        }
    }
}